package com.ideabove.heart.manager;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

import com.ideabove.heart.data.Episode;
import com.ideabove.heart.game.AlarmOffGirlState;
import com.ideabove.heart.game.AlarmOnGirlState;
import com.ideabove.heart.game.EpPlayState;
import com.ideabove.heart.game.ShopAfterState;
import com.ideabove.heart.game.ShopBeforeState;
import com.ideabove.heart.game.StoryEndingState;
import com.ideabove.heart.game.StoryGirlState;
import com.ideabove.heart.game.StoryOffState;
import com.ideabove.heart.game.framework.GameView;
import com.ideabove.heart.game.framework.IState;
import com.ideabove.heart.util.LogUtil;
import com.ideabove.heart.util.MyUtil;

public class MoeManager {
	public static Context mCurContext;
	public static float screenWidth = 0;
	public static float screenHeight = 0;
	public static int DEFAULT_SC_WIDTH = 480;
	public static int DEFAULT_SC_HEIGHT = 800;
	public static String IMG_PATH = "images_480/";
	private static IState curState;
	private static Hashtable<String,Bitmap> imgPool = new Hashtable<String, Bitmap>();	
	
	public static int doit = 0;
	public static int DO_NOTHING = 0;
	public static int DO_STORY = 1;
	public static int DO_STORY_OFF = 2;
	public static int DO_INTRO = 3;
	public static int DO_CHAT = 4;
	public static int DO_CHAT_NO_ANSWER = 5;
	public static int DO_ALARMSET = 6;
	public static int DO_EP_PLAY = 7;
	public static int MORNING_CALL_OFF = 8;
	public static int DO_SHOP_CLOSET = 9;
	public static int DO_SHOP_CLOSET_AFTER = 10;
	public static int DO_FAIL_CALL = 11;
	public static int DO_ENDING = 12;
	
	private static String epName;
	
	public static void setWhatWillIdo(int doit){
		MoeManager.doit = doit;
	}
	
	public static void setWhatWillIdo(int doit, String ep){
		MoeManager.doit = doit;
		MoeManager.epName = ep;
	}
	
	public static void addImg(String key){
		
		if(!imgPool.containsKey(key)){
			Bitmap img = MoeManager.loadDataFromAsset(key);
			if(img != null){ imgPool.put(key, img); }
		}
	}
	
	public static void addImgs(String key){
		
		StringTokenizer st = new StringTokenizer(key,"#");
		
		while(st.hasMoreElements()){
			String token = st.nextToken();
			if(!imgPool.containsKey(token)){
				Bitmap img = MoeManager.loadDataFromAsset(token);				
				if(img != null){ 
					imgPool.put(token, img);
				}
			}
		}
		
	}
	
	public static void addImg(String key, Context context){
		
		if(!imgPool.containsKey(key)){
			Bitmap img = MoeManager.loadDataFromAsset(key, context);			
			if(img != null){ imgPool.put(key, img); }
		}
	}
	
	public static void clearImages(){		
		
		if(imgPool != null){
			Enumeration<Bitmap> bitmp = imgPool.elements();
			
			while(bitmp.hasMoreElements()){				
				Bitmap mp = bitmp.nextElement();
				mp.recycle();
			}
			
			imgPool.clear();			
			System.gc();
			
			
			imgPool = new Hashtable<String, Bitmap>();
		}
	}
	
	
	public static Bitmap getImg(String key){
		return imgPool.get(key);
	}
	
	public static ArrayList<Bitmap> getImgs(String key){
		
		ArrayList<Bitmap>arr = new ArrayList<Bitmap>();
		
		StringTokenizer st = new StringTokenizer(key,"#");		
		while(st.hasMoreElements()){
			String token = st.nextToken();
			arr.add(imgPool.get(token));
		}		
		return arr;
	}

	public static IState getGameState(Context context, GameView gview){
		mCurContext = context;
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		screenWidth = display.getWidth();
		screenHeight = display.getHeight();
		
		if(MoeManager.screenWidth <= MoeManager.DEFAULT_SC_WIDTH){
			IMG_PATH = "images_480/";
		}else{ // 720
			IMG_PATH = "images_720/";
		}

		// 스케쥴 메니져로 부터 얻어온 에피소드 정보를 가지고 케릭터 가져와서 넘긴다.
		return loadGirlStateFromEp(gview);
	}
	
	// 분기점 분기점 분기점
	private static IState loadGirlStateFromEp(GameView gview){

		ScheduleManager scheMgr = new ScheduleManager((Activity)mCurContext);				
		
		// 이미지 지우기
		MoeManager.clearImages();
		
		if(doit == DO_CHAT){ // 메인모드
			int rnum = MyUtil.getRandomNumber(1, 3);
			Episode ep = scheMgr.getEpisodeInfo(mCurContext,"main" + rnum);		
			curState = new StoryOffState(gview, ep);
			
		}else if(doit == DO_CHAT_NO_ANSWER){ // 26 일째부터 전화를 받지 않는다.
			Episode ep = scheMgr.getEpisodeInfo(mCurContext,"main4");		
			curState = new StoryOffState(gview, ep);
			
		}else if(doit == DO_ALARMSET){ // 알람셋하기			
			Episode ep = scheMgr.getEpisodeInfo(mCurContext,"alarmset");
			curState = new AlarmOnGirlState(gview, ep);
			
		}else if(doit == MORNING_CALL_OFF){			
			Episode ep = scheMgr.getEpisodeInfo(mCurContext,"alarmoff");
			curState = new AlarmOffGirlState(gview, ep);
			
		}else if(doit == DO_INTRO){
			Episode ep = scheMgr.getEpisodeInfo(mCurContext,"ep0"); 
			curState = new StoryGirlState(gview, ep);
			
		}else if(doit == DO_STORY){ // 스토리모드
			Episode ep = scheMgr.getEpisodeInfo(mCurContext);
			curState = new StoryGirlState(gview, ep);
			
		}else if(doit == DO_EP_PLAY){ // 지난 에피소드 보기
			Episode ep = scheMgr.getEpisodeInfo(mCurContext, epName);
			curState = new EpPlayState(gview, ep);
			
		}else if(doit == DO_SHOP_CLOSET){ // 옷갈아 입히기
			Episode ep = scheMgr.getEpisodeInfo(mCurContext, "shop");
			curState = new ShopBeforeState(gview, ep);
			
		}else if(doit == DO_SHOP_CLOSET_AFTER){ // 옷갈아 입힌후
			Episode ep = scheMgr.getEpisodeInfo(mCurContext, "shop_after");
			curState = new ShopAfterState(gview, ep);
			
		}else if(doit == DO_STORY_OFF){ // 스토리 off 모닝콜
			int rnum = MyUtil.getRandomNumber(1, 5);
			Episode ep = scheMgr.getEpisodeInfo(mCurContext, "storyoff" + rnum);
			curState = new StoryOffState(gview, ep);
			
		}else if(doit == DO_FAIL_CALL){ // 부재중
			int rnum = MyUtil.getRandomNumber(1, 3);
			Episode ep = scheMgr.getEpisodeInfo(mCurContext, "failcall" + rnum);
			curState = new StoryOffState(gview, ep);
			
		}else if(doit == DO_ENDING){ // 엔딩씬
			Episode ep = scheMgr.getEpisodeInfo(mCurContext, "ep31");
			curState = new StoryEndingState(gview, ep);
		}
		return curState;
	}
	
	public static IState getCurrentState(){
		return curState;
	}


	public static Bitmap loadDataFromAsset(String fname) {		
		// load image		
		try {
			// get input stream			 
			 InputStream ims = mCurContext.getAssets().open(IMG_PATH + fname);
			 Bitmap bitmap = BitmapFactory.decodeStream(ims);
			 ims.close();
			return  bitmap;
		}
		catch(IOException ex) {
			return null;
		}catch(Error e){
			Toast.makeText(mCurContext, "메모리가 부족합니다.", Toast.LENGTH_SHORT).show();
			return null;
		}
	}
	
	public static Bitmap loadDataFromAsset(String fname,Context context) {		
		// load image
		try {
			// get input stream
			InputStream ims = context.getAssets().open(IMG_PATH + fname);
			Bitmap bitmap = BitmapFactory.decodeStream(ims);
			 ims.close();
			return bitmap;
		}
		catch(IOException ex) {
			return null;
		}catch(Error e){
			Toast.makeText(mCurContext, "메모리가 부족합니다.", Toast.LENGTH_SHORT).show();
			return null;
		}
	}
}
